package com.wyl.java.juc_.reentranlock_;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 吴宇亮 on 2023/3/28 14:08
 */
public class ReentrantLock01_Counter extends Thread {

    private Integer num = 0;
    private ReentrantLock reentrantLock = new ReentrantLock();

    public void add () {
        reentrantLock.lock();
        try {
            num++;
        } finally {
            reentrantLock.unlock();
        }
    }

    public void sub () {
        reentrantLock.lock();
        try {
            num--;
        } finally {
            reentrantLock.unlock();
        }
    }

    public void setNum (Integer i) {
        reentrantLock.lock();
        try {
            this.num = i;
        } finally {
            reentrantLock.unlock();
        }
    }

    public Integer getNum () {
        reentrantLock.lock();
        try {
            return num;
        } finally {
            reentrantLock.unlock();
        }
    }

}
